#include <chat.h>
#include <interface.h>

#include <iostream>


using namespace std;
using namespace Chat;

void* ClientRecvMsg(void* sockfd)
{
    int connfd = *(int*)sockfd;
    int nRead;

    char    buf[MAX_LINE]{}, str[MAX_LINE]{};
    Message message{};

    time_t timep;

    printf("^_^ 接收聊天信息中～\n");
    while (1) {
        /*接收服务器发来的消息*/
        nRead = recv(connfd, buf, sizeof(message), 0);
        /*recv函数返回值 <0 出错  =0 链接关闭  >0接收到的字节数*/
        if (nRead <= 0) {
            printf("您已经异常掉线，请重新登录！\n");
            close(connfd);
            exit(0);
        }   // if

        memset(&message, 0, sizeof(message));
        memcpy(&message, buf, sizeof(buf));

        switch (message.msgType) {
        case MessageType::PERSONAL_CHAT:
            sprintf(str,
                    "%s \t %s \t对你说： %s\n",
                    message.sendName,
                    message.msgTime,
                    message.content);
            printf("\n%s\n", str);
            break;

        case MessageType::GROUP_CHAT:
            sprintf(str,
                    "%s \t %s \t发送群消息： %s\n",
                    message.sendName,
                    message.msgTime,
                    message.content);
            printf("\n%s\n", str);
            break;

        case MessageType::RESULT:
            printf("你的操作结果：%s\n", message.content);
        default:
            break;
        }
    }
}



void ClientEnterChat(const User& user, int sockfd)
{
    int     choice, ret;
    char    c, buf[MAX_LINE]{}, str[MAX_LINE]{};
    Message message{}; /*消息对象*/
    time_t  timep;     /*存储当前时间*/

    pthread_t pid;     /*处理接收消息线程*/

                       /*创建接收消息线程*/
    ret = pthread_create(&pid, NULL, ClientRecvMsg, (void*)&sockfd);
    if (ret != 0) {
        printf("软件异常，请重新登录！\n");
        message = Message{};
        strcpy(message.sendName, user.userName);
        message.msgType = MessageType::EXIT;
        send(sockfd, buf, sizeof(buf), 0);
        close(sockfd);
        exit(1);
    }
    /*清空标准输入缓冲区*/
    setbuf(stdin, NULL);

    /*进入处理用户发送消息缓冲区*/
    while (1) {
        message = Message{};
        strcpy(message.sendName, user.userName);
        memset(&str, 0, MAX_LINE);
        memset(buf, 0, MAX_LINE);
        /*usleep函数将该进程挂起一定时间，单位微秒，头文件unistd.h*/
        usleep(100000);

        /*进入聊天主界面*/
        chatInterface(user.userName);
        setbuf(stdin, NULL);   // 是linux中的C函数，主要用于打开和关闭缓冲机制
        scanf("%d", &choice);
        while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5) {
            printf("未知操作，请重新输入！\n");
            setbuf(stdin, NULL);   // 是linux中的C函数，主要用于打开和关闭缓冲机制
            scanf("%d", &choice);
            setbuf(stdin, NULL);
        }   // while

        string input, total_input = "";

        switch (choice) {
        case 1: /*查看当前在线用户列表*/
            message.msgType = MessageType::VIEW_USER_LIST;
            memcpy(buf, &message, sizeof(message));
            send(sockfd, buf, sizeof(buf), 0);
            break;

        case 2: /*私聊*/
            message.msgType = MessageType::PERSONAL_CHAT;
            printf("请输入聊天对象：\n");
            setbuf(stdin, NULL);
            scanf("%s", str);
            strcpy(message.recvName, str);

            printf("请输入聊天内容：\n");
            setbuf(stdin, NULL);
            input.clear();
            total_input.clear();
            while (getline(cin, input)) {
                total_input += input + "\n";
            }

            cin.clear();
            cin.sync();
            clearerr(stdin);

            strcpy(message.content, total_input.c_str());
            (message.content)[strlen(message.content) - 1] = '\0';
            /*获得当前时间*/
            time(&timep);
            strcpy(message.msgTime, ctime(&timep));
            memcpy(buf, &message, sizeof(message));
            send(sockfd, buf, sizeof(buf), 0);
            break;

        case 3: /*群聊*/
            message.msgType = MessageType::GROUP_CHAT;
            strcpy(message.recvName, "");

            printf("请输入聊天内容：\n");
            input.clear();
            total_input.clear();
            while (getline(cin, input)) {
                total_input += input + "\n";
            }

            cin.clear();
            cin.sync();
            clearerr(stdin);

            strcpy(message.content, total_input.c_str());
            // setbuf(stdin, NULL);
            // fgets(message.content, sizeof(message.content), stdin);
            (message.content)[strlen(message.content) - 1] = '\0';

            /*获得当前时间*/
            time(&timep);
            strcpy(message.msgTime, ctime(&timep));
            memcpy(buf, &message, sizeof(message));
            send(sockfd, buf, sizeof(buf), 0);
            break;
        case 4: /*查看聊天记录*/
            break;
        case 5: /*退出登陆*/
            message.msgType = MessageType::EXIT;
            memcpy(buf, &message, sizeof(message));
            send(sockfd, buf, sizeof(buf), 0);
            close(sockfd);
            exit(0);
            break;

        default: /*未知操作类型*/
            break;
        }
    }
}
